package pers.xx.sbm.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.utils.UserUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ：xiexing
 * @description：拦截器逻辑设置
 * @date ：2020/11/26 17:20
 */
@Component
public class ConfigInterceptor implements HandlerInterceptor {

    /**
     * slf4j日志
     */
    private final static Logger logger = LoggerFactory.getLogger(ConfigInterceptor.class);

    @Autowired
    private UserUtils userUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从session中获取用户信息
        User user = userUtils.getCurrentUser();
        // session过期
        if (user == null) {
            logger.info("session过期, 跳至登录页");
            response.sendRedirect("/toLogin"); // 通过接口跳转登录页面, 注:重定向后下边的代码还会执行 ; /outToLogin是跳至登录页的后台接口
            return false;
        } else {
            return true;
        }

    }
}
